package 功能实现.管理员角色权限三级.controller;

import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.passport.pojo.param.PermissionAddNewParam;
import cn.tedu.autoshow.passport.pojo.param.PermissionUpdateParam;
import cn.tedu.autoshow.passport.pojo.vo.PermissionListItemVO;
import cn.tedu.autoshow.passport.pojo.vo.PermissionStandardVO;
import cn.tedu.autoshow.passport.service.IPermissionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/permissions")
@Api(tags = "03. 权限管理模块")
@Slf4j
@Validated
public class PermissionController {

    @Autowired
    private IPermissionService service;
    
    @PostMapping("/add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PermissionAddNewParam permissionAddNewParam){
        service.addNew(permissionAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除权限")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "Long")
    })
    public JsonResult deleteById(
            @Range(min = 1, message = "根据ID删除权限失败，请提交合法的ID值！")
            @PathVariable Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新权限")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新权限失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid PermissionUpdateParam permissionUpdateParam){
        service.updateInfoById(id, permissionUpdateParam);

        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询权限详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询权限失败，请提交合法的ID值！")
            @PathVariable Long id){
        PermissionStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("")
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "查询权限列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<PermissionListItemVO> pageData ;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.list(pageNum);
        }

        return JsonResult.ok(pageData);
    }
    
}
